<?php
namespace trade\common\gather;

use trade\common\service\KlineService;
class GatherRandom extends Gather
{
    public function run($tables,$prex='trade'){
        $this->loadSymbol($tables,function($items) use ($prex)
        {
            $time   = time();
            foreach ($items as $item){
                $kline  = new KlineService();
                $ticket = $kline->loadTicekt($item['code'],$prex);
                if(empty($ticket)){
                    $item['ticket'] = [
                        'ask'        => 1000,//卖一价
                        'asm'        => 58,//卖一量
                        'bid'        => 1000,//买一价
                        'bim'        => 58,//买一量
                        'ask2'       => 1000,//卖二价
                        'asm2'       => 60,//卖二量
                        'bid2'       => 1000,//买二价
                        'bim2'       => 70,//买二量
                        'ask3'       => 1000,//卖三价
                        'asm3'       => 46,//卖三量
                        'bid3'       => 100,//买三价
                        'bim3'       => 59,//买三量
                        'ask4'       => 1000,//卖四价
                        'asm4'       => 93,//卖四量
                        'bid4'       => 1000,//买四价
                        'bim4'       => 23,//买四量
                        'ask5'       => 1000,//卖五价
                        'asm5'       => 10,//卖五量
                        'bid5'       => 1000,//买五价
                        'bim5'       => 60,//买五量
                        'price'      => 1000,
                        //'price2'     => 0,
                        'volume'     => 93243,
                        'position'   => 1032839,
                        'open'       => 1000,//开盘价
                        'close'      => 1000,//收盘价
                        'high'       => 1000,//最高价
                        'low'        => 1000,//最低价
                        'wave'       => 0,//涨幅
                        'cleaning'   => 0,//平仓量
                        'limit_up'   => 0,//涨停
                        'limit_dn'   => 0,//跌停
                        'code'       => $item['code'],
                        'digit'      => 4,
                        'ctm'        => $time,
                        'ctmfmt'     => date('Y-m-d H:i:s',$time)
                    ];
                }else{
                    $item['ticket'] = [
                        'ask'        => $this->randomPrice($ticket['ask']),//卖一价
                        'asm'        => $this->randomPrice($ticket['asm']),//卖一量
                        'bid'        => $this->randomPrice($ticket['bid']),//买一价
                        'bim'        => $this->randomPrice($ticket['bim']),//买一量
                        'ask2'       => $this->randomPrice($ticket['ask2']),//卖二价
                        'asm2'       => $this->randomPrice($ticket['asm2']),//卖二量
                        'bid2'       => $this->randomPrice($ticket['bid2']),//买二价
                        'bim2'       => $this->randomPrice($ticket['bim2']),//买二量
                        'ask3'       => $this->randomPrice($ticket['ask3']),//卖三价
                        'asm3'       => $this->randomPrice($ticket['asm3']),//卖三量
                        'bid3'       => $this->randomPrice($ticket['bid3']),//买三价
                        'bim3'       => $this->randomPrice($ticket['bim3']),//买三量
                        'ask4'       => $this->randomPrice($ticket['ask4']),//卖四价
                        'asm4'       => $this->randomPrice($ticket['asm4']),//卖四量
                        'bid4'       => $this->randomPrice($ticket['bid4']),//买四价
                        'bim4'       => $this->randomPrice($ticket['bim4']),//买四量
                        'ask5'       => $this->randomPrice($ticket['ask5']),//卖五价
                        'asm5'       => $this->randomPrice($ticket['asm5']),//卖五量
                        'bid5'       => $this->randomPrice($ticket['bid5']),//买五价
                        'bim5'       => $this->randomPrice($ticket['bim5']),//买五量
                        'price'      => $this->randomPrice($ticket['price']),
                        //'price2'     => $this->randomPrice($ticket['price2']),
                        'volume'     => $this->randomPrice($ticket['volume']),
                        'position'   => $this->randomPrice($ticket['position']),
                        'open'       => $this->randomPrice($ticket['open']),//开盘价
                        'close'      => $this->randomPrice($ticket['close']),//收盘价
                        'high'       => $this->randomPrice($ticket['high']),//最高价
                        'low'        => $this->randomPrice($ticket['low']),//最低价
                        'wave'       => $this->randomPrice($ticket['wave']),//涨幅
                        'cleaning'   => $this->randomPrice($ticket['cleaning']),//平仓量
                        'limit_up'   => $this->randomPrice($ticket['limit_up']),//涨停
                        'limit_dn'   => $this->randomPrice($ticket['limit_dn']),//跌停
                        'code'       => $item['code'],
                        'digit'      => 4,
                        'ctm'        => $time,
                        'ctmfmt'     => date('Y-m-d H:i:s',$time)
                    ];
                }
                $this->_listen($item,$prex);
            }
        },100);
    }

    public function randomPrice($price = 1000, $float = 1)
    {
        $price = $this->randomFloat(bcsub($price, $float, 4), bcadd($price, $float, 4), 4);

        return $price < 1
            ? (rand(1, 100) / 100)
            : $price;
    }

    public function randomFloat($min = 0, $max = 1)
    {
        return round($min + mt_rand() / mt_getrandmax() * ($max - $min), 4);
    }
}